
 ***********************************************************************************
 *. Importing raw data 
 ********************************************************************************
 
 *raw data provided by the ministry of labor 
 import delimited C:\Users\Public\Documents\Data\SIPSI\dpd_sipsi.csv, clear 
 save "C:\Users\Public\Documents\Data\SIPSI\dpd.dta", replace
 
 *raw data exported from sas to dta 
 use "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta", clear 
 drop if missing(denom)
 duplicates tag denom, g(flag)
 sort denom siren
 bysort denom:keep if _n==1
 drop flag 
 save "C:\Users\Public\Documents\Data\FICUS\ficus2018name.dta"
 
 use "C:\Users\Public\Documents\Data\FICUS\ficus2017.dta", clear 
 drop if missing(denom)
 duplicates tag denom, g(flag)
 sort denom siren
 bysort denom:keep if _n==1
 drop flag 
 save "C:\Users\Public\Documents\Data\FICUS\ficus2017name.dta"
 
 use "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta", clear
 egen numid=group(siren)
 save "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta", replace
 
 use "C:\Users\Public\Documents\Data\FICUS\ficus2017.dta", clear
 egen numid=group(siren)
 save "C:\Users\Public\Documents\Data\FICUS\ficus2017.dta", replace
 
 ***********************************************************************************
 *. Baseline dataset from RAW SIPSI 
 ******************************************************************************** 
 
  import delimited C:\Users\Public\Documents\Data\SIPSI\Salaries_sipsi.csv, clear 
  gen debut=date(detach_date_debut, "DMY")
  gen fin=date(detach_date_fin, "DMY")
  gen contrat=date(date_signature_contrat,"DMY")
  format debut %td
  format fin %td
  format contrat %td
  g duration=fin-debut
  replace duration=1 if debut==fin
  g year=substr(detach_date_debut,7,10)
  destring year, replace
  g freq=1
  
*There are discrepancies in the way foreign firms fill the wage variable. Most companies appear to fill monthly basis rates for their employees (85% fill wages > 1000 euros, including for contracts that last only one day). There is large bunching at the French monthly legal wage (1498 in 2018), but also smaller bunching at the French minimum legal wage (9.88/hour which is 1498/151/7). Since 2021 (outside this period of stuy), the SIPSI reporting system has been modified and ask explicitely companies to fill hourly wage rates, and to convert the monthly rates of their employees into gross hourly rates when filling the SIPSI form. 
 
*I convert the monthly equivalent basis rate into hourly equivalent when raw wage filled is more than 500euros,
*using the French legal basis of 151.7 hours per month that is used to compute the enforcement of the minimum legal wage (that is binding for foreign firms). 
 
  g salaire_r=.
  replace salaire_r=remuneration/151.7 if remuneration>500
  
*For wages below 200 per hours, I assume those are already hourly rates and leave the raw data (*Only 0.8% of observations have a reported wage between 100 and 200 euros)

  replace salaire_r=remuneration if remuneration<200
  
*Origin Wages in 200-500 range account for less than 1% of remaining cases-- do not take a stance and leave as missing 

  g age=year-annee_naissance
  merge m:1 numenvoi using "C:\Users\Public\Documents\Data\SIPSI\dpd.dta"
  *3% unmatched 
  rename _m matched_dpd
  compress
  *winsorize to get rid of some extreme values driven by reporting mistakes (few hourly wages are in the million/billion euros, even though the workers are classified as blue collars)
  winsor salaire_r, p(0.01) gen(salaire_r_w)
  drop salaire_r
  rename salaire_r_w salaire_r
  save "C:\Users\Public\Documents\Data\SIPSI\dpd_salarie.dta", replace
  
  
 ***********************************************************************************
 *. Matching algorithm posting data: year 2017
 ********************************************************************************

  use "C:\Users\Public\Documents\Data\SIPSI\dpd_salarie.dta", clear 
  tostring do_siren, replace
  replace do_siren="." if missing(do_siren)
  replace do_siren="." if do_siren=="0"
  replace do_numtva="." if missing(do_numtva)
  replace do_numtva="." if do_numtva=="FR00000000000000"
  g siren_old=do_siren
  g udo_denomination=upper(do_denomination)
  sort udo_denomination do_siren
  order udo_denomination do_siren do_numtva
  by udo_denomination: replace do_siren=do_siren[_N] if do_siren=="." & do_siren[_N]!="."
  sort do_numtva do_siren
  by do_numtva: replace do_siren=do_siren[_N] if do_siren=="." & do_numtva[_N]!="."
  collapse (sum) freq, by(year do_siren do_numtva udo_denomination)
  gen l1=length(do_siren)
  g do_siren_2=substr(do_numtva,3,9)
  g do_siren_3=substr(do_numtva,5,11)
  drop if year==2019
  keep if year==2017
  rename do_siren siren
  *first match: based on do_siren
  merge m:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2017.dta"
  g status="."
  replace status="step1" if _m==3
  drop if _m==2
  replace do_siren_2="." if status=="step1"
  rename siren siren1
  rename do_siren_2 siren
  drop _m
  merge m:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2017.dta", update
  drop if _m==2
  tab _m if status=="step1"
  replace status="step2" if _m==4
  drop _m
  replace do_siren_3="." if status=="step2"
  replace do_siren_3="." if status=="step1"
  rename siren siren2
  rename do_siren_3 siren
  merge m:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2017.dta", update
  drop if _m==2
  replace status="step3" if _m==4
  drop _m
  *name
  g old_name=udo_denomination
  rename denom old_denom
  rename udo_denomination denom
  replace denom="." if status!="."
  replace denom="." if denom==""
  rename siren siren_old
  merge m:1 denom using "C:\Users\Public\Documents\Data\FICUS\ficus2017name.dta", update
  drop if _m==2
  replace status="step4" if _m==4
  drop _m
  drop if status=="."
   g siren_def="."
   replace siren_def=siren1 if status=="step1"
   replace siren_def=siren2 if status=="step2"
   replace siren_def=siren_old if status=="step3"
   replace siren_def=siren if status=="step4"
   g name="."
   replace name=denom if status=="step4"
   sort siren_def name
   by siren_def: replace name=name[_N] if name=="." & name[_N]!="."
   replace name=old_name if name=="." 
   duplicates tag siren_def, g(flagi)
   sort siren_def
   duplicates drop siren_def, force
   drop siren1 siren2 siren_old siren
   rename siren_def siren
   merge 1:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2017.dta"
   g user=1 if _m==3
   g freqi=1
   keep if user==1
   drop _m
   save "C:\Users\Public\Documents\Data\SIPSI\final_users_2017.dta", replace
   *list of users succesfully matched, with good name and Siren from matching 
   
  use "C:\Users\Public\Documents\Data\SIPSI\final_users_2017.dta", clear 
  drop if missing(name)
  duplicates tag name, g(flag3)
  keep if status=="step4"
  duplicates tag name, g(flag4)
  save "C:\Users\Public\Documents\Data\SIPSI\final_users_2017name.dta"

  use "C:\Users\Public\Documents\Data\SIPSI\dpd_salarie.dta", clear 
  tostring do_siren, replace
  replace do_siren="." if missing(do_siren)
  replace do_siren="." if do_siren=="0"
  replace do_numtva="." if missing(do_numtva)
  replace do_numtva="." if do_numtva=="FR00000000000000"
  g siren_old=do_siren
  g udo_denomination=upper(do_denomination)
  sort udo_denomination do_siren
  order udo_denomination do_siren do_numtva
  by udo_denomination: replace do_siren=do_siren[_N] if do_siren=="." & do_siren[_N]!="."
  sort do_numtva do_siren
  by do_numtva: replace do_siren=do_siren[_N] if do_siren=="." & do_numtva[_N]!="."
  rename do_siren siren
  keep if year==2017
  g do_siren_2=substr(do_numtva,3,9)
  g do_siren_3=substr(do_numtva,5,11)
  drop freq
  drop year
  rename do_numtva do_numtva2
  merge m:1 siren using "C:\Users\Public\Documents\Data\SIPSI\final_users_2017.dta"
  drop if _m==2
  g status2="."
  replace status2="step1" if _m==3
  replace do_siren_2="." if status2=="step1"
  rename siren siren1 
  rename do_siren_2 siren
  drop _m
  merge m:1 siren using "C:\Users\Public\Documents\Data\SIPSI\final_users_2017.dta", update
  drop if _m==2
  replace status2="step2" if _m==4
  rename siren siren2
  replace do_siren_3="." if status2=="step2"
  replace do_siren_3="." if status2=="step1"
  rename do_siren_3 siren 
  drop _m
  merge m:1 siren using "C:\Users\Public\Documents\Data\SIPSI\final_users_2017.dta", update
  drop if _m==2
  replace status2="step3" if _m==4
  rename siren siren3
  drop name
  rename udo_denomination name
  replace name="." if status2!="."
  drop _m
  merge m:1 name using "C:\Users\Public\Documents\Data\SIPSI\final_users_2017name.dta", update
  replace status2="step4" if _m==4
  drop if _m==2
  drop _m
  replace status2="step5" if secteur_do_sirene_a4!="" & status2=="."
  g part1=substr(ape_diff,1,2)
  g part2=substr(ape_diff,3,5)
  g pt="."
  g ape_do=part1+pt+part2
  replace ape_do=secteur_do_sirene_a4 if status2=="step5" 
  save "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpd2017.dta", replace
  
  use "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpd2017.dta", clear
  *convert dayly to hourly duration
  g pw_wagebill=salaire_r*duration*10
  g nbheur_posted=duration*10
  g count=1
  g siren_def="."
   replace siren_def=siren1 if status2=="step1"
   replace siren_def=siren2 if status2=="step2"
   replace siren_def=siren3 if status2=="step3"
   replace siren_def=siren if status2=="step4"
   replace siren_def=siren_old if status2=="step5"

   g do_name="."
   replace do_name=name if status2=="step4"
   replace do_name=old_denom if status2=="step1"
   replace do_name=old_denom if status2=="step2"
   replace do_name=old_denom if status2=="step3"
   replace do_name=name if status2=="."
   *nombre d'uniques clients francais 
  count
  count if siren_def=="."
  count if siren_def=="." & missing(do_denomination)
  preserve 
 * replace siren_def=siren_old if siren_def=="."  & status2=="." & siren_old!="."
  collapse (sum) count duration pw_wagebill, by(siren_def name)
  count
  count if siren_def!="."
  g indic=0
  replace indic=1 if siren_def!="."
  collapse (sum) count duration pw_wagebill, by(indic)
  egen tot_w=sum(pw_wagebill)
  egen tot_s=sum(count)
  g s=count/tot_s
  g z=pw_wagebill/tot_w
  restore

  g indic=0
  replace indic=1 if siren_def=="."
  bys indic: su duration
  bys indic: tab secteur_redresse
  drop if siren_def=="."
  collapse (sum) count pw_wagebill nbheur_posted (mean) salaire_r, by(siren_def)
  rename count pw_count 
  save "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpdcollapsed2017.dta", replace

   
 ***********************************************************************************
 *. Matching algorithm posting data: year 2018 
 ********************************************************************************

  use "C:\Users\Public\Documents\Data\SIPSI\dpd_salarie.dta", clear 
  tostring do_siren, replace
  replace do_siren="." if missing(do_siren)
  replace do_siren="." if do_siren=="0"
  replace do_numtva="." if missing(do_numtva)
  replace do_numtva="." if do_numtva=="FR00000000000000"
  g siren_old=do_siren
  g udo_denomination=upper(do_denomination)
  sort udo_denomination do_siren
  order udo_denomination do_siren do_numtva
  by udo_denomination: replace do_siren=do_siren[_N] if do_siren=="." & do_siren[_N]!="."
  sort do_numtva do_siren
  by do_numtva: replace do_siren=do_siren[_N] if do_siren=="." & do_numtva[_N]!="."
  collapse (sum) freq, by(year do_siren do_numtva udo_denomination)
  gen l1=length(do_siren)
  g do_siren_2=substr(do_numtva,3,9)
  g do_siren_3=substr(do_numtva,5,11)
  drop if year==2019
  keep if year==2018
  rename do_siren siren
  *first match: based on do_siren
  merge m:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta"
  g status="."
  replace status="step1" if _m==3
  drop if _m==2
  replace do_siren_2="." if status=="step1"
  rename siren siren1
  rename do_siren_2 siren
  drop _m
  merge m:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta", update
  drop if _m==2
  tab _m if status=="step1"
  replace status="step2" if _m==4
  drop _m
  replace do_siren_3="." if status=="step2"
  replace do_siren_3="." if status=="step1"
  rename siren siren2
  rename do_siren_3 siren
  merge m:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta", update
  drop if _m==2
  replace status="step3" if _m==4
  drop _m
  *name
  g old_name=udo_denomination
  rename denom old_denom
  rename udo_denomination denom
  replace denom="." if status!="."
  replace denom="." if denom==""
  rename siren siren_old
  merge m:1 denom using "C:\Users\Public\Documents\Data\FICUS\ficus2018name.dta", update
  drop if _m==2
  replace status="step4" if _m==4
  drop _m
  drop if status=="."
   g siren_def="."
   replace siren_def=siren1 if status=="step1"
   replace siren_def=siren2 if status=="step2"
   replace siren_def=siren_old if status=="step3"
   replace siren_def=siren if status=="step4"
   g name="."
   replace name=denom if status=="step4"
   sort siren_def name
   by siren_def: replace name=name[_N] if name=="." & name[_N]!="."
   replace name=old_name if name=="." 
   duplicates tag siren_def, g(flagi)
   sort siren_def
   duplicates drop siren_def, force
   drop siren1 siren2 siren_old siren
   rename siren_def siren
   merge 1:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta"
   g user=1 if _m==3
   g freqi=1
   keep if user==1
   drop _m
   save "C:\Users\Public\Documents\Data\SIPSI\final_users_2018.dta", replace
   
  use "C:\Users\Public\Documents\Data\SIPSI\final_users_2018.dta", clear 
  drop if missing(name)
  duplicates tag name, g(flag3)
  keep if status=="step4"
  duplicates tag name, g(flag4)
  save "C:\Users\Public\Documents\Data\SIPSI\final_users_2018name.dta"

  use "C:\Users\Public\Documents\Data\SIPSI\dpd_salarie.dta", clear 
  tostring do_siren, replace
  replace do_siren="." if missing(do_siren)
  replace do_siren="." if do_siren=="0"
  replace do_numtva="." if missing(do_numtva)
  replace do_numtva="." if do_numtva=="FR00000000000000"
  g siren_old=do_siren
  g udo_denomination=upper(do_denomination)
  sort udo_denomination do_siren
  order udo_denomination do_siren do_numtva
  by udo_denomination: replace do_siren=do_siren[_N] if do_siren=="." & do_siren[_N]!="."
  sort do_numtva do_siren
  by do_numtva: replace do_siren=do_siren[_N] if do_siren=="." & do_numtva[_N]!="."
  rename do_siren siren
  keep if year==2018
  g do_siren_2=substr(do_numtva,3,9)
  g do_siren_3=substr(do_numtva,5,11)
  drop freq
  drop year
  rename do_numtva do_numtva2
  merge m:1 siren using "C:\Users\Public\Documents\Data\SIPSI\final_users_2018.dta"
  drop if _m==2
  g status2="."
  replace status2="step1" if _m==3
  replace do_siren_2="." if status2=="step1"
  rename siren siren1 
  rename do_siren_2 siren
  drop _m
  merge m:1 siren using "C:\Users\Public\Documents\Data\SIPSI\final_users_2018.dta", update
  drop if _m==2
  replace status2="step2" if _m==4
  rename siren siren2
  replace do_siren_3="." if status2=="step2"
  replace do_siren_3="." if status2=="step1"
  rename do_siren_3 siren 
  drop _m
  merge m:1 siren using "C:\Users\Public\Documents\Data\SIPSI\final_users_2018.dta", update
  drop if _m==2
  replace status2="step3" if _m==4
  rename siren siren3
  drop name
  rename udo_denomination name
  replace name="." if status2!="."
  drop _m
  merge m:1 name using "C:\Users\Public\Documents\Data\SIPSI\final_users_2018name.dta", update
  replace status2="step4" if _m==4
  drop if _m==2
  drop _m
  replace status2="step5" if secteur_do_sirene_a4!="" & status2=="."
  g part1=substr(ape_diff,1,2)
  g part2=substr(ape_diff,3,5)
  g pt="."
  g ape_do=part1+pt+part2
  replace ape_do=secteur_do_sirene_a4 if status2=="step5" 
  save "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpd.dta", replace
  
  use "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpd.dta", clear
  g pw_wagebill=salaire_r*duration*10
  g nbheur_posted=duration*10
  g count=1
  g siren_def="."
   replace siren_def=siren1 if status2=="step1"
   replace siren_def=siren2 if status2=="step2"
   replace siren_def=siren3 if status2=="step3"
   replace siren_def=siren if status2=="step4"
   replace siren_def=siren_old if status2=="step5"

   g do_name="."
   replace do_name=name if status2=="step4"
   replace do_name=old_denom if status2=="step1"
   replace do_name=old_denom if status2=="step2"
   replace do_name=old_denom if status2=="step3"
   replace do_name=name if status2=="."
   *nombre d'uniques clients francais 
  count
  count if siren_def=="."
  count if siren_def=="." & missing(do_denomination)
  preserve 
 * replace siren_def=siren_old if siren_def=="."  & status2=="." & siren_old!="."
  collapse (sum) count duration pw_wagebill, by(siren_def name)
  count
  count if siren_def!="."
  g indic=0
  replace indic=1 if siren_def!="."
  collapse (sum) count duration pw_wagebill, by(indic)
  egen tot_w=sum(pw_wagebill)
  egen tot_s=sum(count)
  g s=count/tot_s
  g z=pw_wagebill/tot_w
  restore
  
  g indic=0
  replace indic=1 if siren_def=="."
  bys indic: su duration
  bys indic: tab secteur_redresse
  
  
  
  drop if siren_def=="."
  collapse (sum) count pw_wagebill nbheur_posted (mean) salaire_r, by(siren_def)
  rename count pw_count 
  save "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpdcollapsed.dta", replace
  
  
  
  ***********************************************************************
  * Creating panel of users 
  ************************************************************************
  
  
  use  "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpdcollapsed2017.dta", clear
  count 
  use  "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpdcollapsed.dta", clear
  count
  
  use  "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpdcollapsed2017.dta", clear
  append using  "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpdcollapsed.dta"
  g freq=1
  collapse (sum) freq, by(siren_def)
  drop freq
  save "C:\Users\Public\Documents\Data\SIPSI\users20172018.dta", replace
  
  
  ***********************************************************************
  * Descriptives on receiving firms 
  ************************************************************************  
  
   *call raw dads data (exported from sas to dta) for year 2018
   use "C:\Users\Public\Documents\Data\ESA\dads2018.dta", clear 
   append using "C:\Users\Public\Documents\Data\ESA\dads20182.dta"
   append using "C:\Users\Public\Documents\Data\ESA\dads20183.dta"
   append using "C:\Users\Public\Documents\Data\ESA\dads20184.dta"
   drop if filt=="2"
   drop if filt_1=="2"
   g interim=1
   replace interim=0 if missing(siret_utilisateur)
   g siren_all=siren_empl 
   replace siren_all="." if interim==1
   gen siren_interim=substr(siret_utilisateur,1,9)
   replace siren_all=siren_interim if interim==1
   count
   drop if missing(s_brut)
   g w=s_brut/nbheur
   g indicmiwage=0
   replace indicmiwage=1 if w>=9.88 & w<9.98
   g part1=substr(apen,1,2)
   g part2=substr(apen,3,5)
   g pt="."
   g ape=part1+pt+part2
   g order=-interim
   sort siren_all order
   by siren_all: replace ape=ape[_N] if interim==1
   rename siren_all siren_def
   merge m:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\users20172018.dta"
   
   g user=0
   replace user=1 if _m==3
   drop if _m==2
   
   preserve 
   g tagix=1
   collapse (sum) nbheur  tagix s_brut, by(apen user)
   
   foreach y in nbheur tagix s_brut{
   egen tot`y'=sum(`y'), by(apen)
   g s`y'=`y'/tot`y'
   }
   restore 
   
   *descriptive stat* 
   
   tab apen if user==1, sort 
   bys user: su w, d 
   bys user: su age, d 
   bys user: su nbheur, d 
   bys user: tab contrat_travail
   
   g logwage=log(w)
   g loghour=log(nbheur)
   
   reghdfe logwage loghour 1.user, absorb(apen)
   reghdfe logwage loghour 1.user, absorb(ident_s apen)
   
   
   twoway (hist w if user==1 & w<50 & w>0,  width(0.1) frac color(blue%30))  (hist w if user==0 & w<50 & w>0,  width(0.1) frac color(red%30)) ///
   , graphregion(fcolor(white) lcolor(white)) legend( label(1 "Using Firm") label(2 "Non Using Firm") ///
  ring(0) pos(1) cols(1)) xtitle("Gross Hourly Wage")
  graph export  "C:\Users\Public\Documents\Data\hist_usernonuserDADS2018.png", replace
  
  
  
   
  g indic_cdd=0
  replace indic_cdd=1 if contrat_travail=="02"
  replace indic_cdd=1 if contrat_travail=="03"
  replace indic_cdd=1 if contrat_travail=="95"
  
  g indicnew=0
  replace indicnew=1 if filt=="1" & filt_1=="" & contrat_travail=="01"
  replace indicnew=1 if filt=="2" & filt_1=="" & contrat_travail=="01"
  
  g incumb=0
  replace incumb=1 if  filt_1=="2" & contrat_travail=="01"
  replace incumb=1 if  filt_1=="1"  & contrat_travail=="01"

  g nbheur_cdd=.
  replace nbheur_cdd=nbheur if indic_cdd==1
  
  g w_cdd=.
  replace w_cdd=w if indic_cdd==1
  
  g w_interim=.
  replace w_interim=w if interim==1
  
  g w_entrant=.
  replace w_entrant=w if indicnew==1
  
  g w_incub=.
  replace w_incub=w if incumb==1
  
  g sbrut_cdd=.
  replace sbrut_cdd=s_brut if indic_cdd==1
  
  g sbrut_interim=.
  replace sbrut_interim=s_brut if interim==1
  
  g nbheur_interim=.
  replace nbheur_interim=nbheur if interim==1
  
  
   g immig=0
   replace immig=1 if dep_naiss=="99"
  
  g w_immig=.
  replace w_immig=w if immig==1
  
  g w_entrant_imm=.
  replace w_entrant_imm=w if indicnew==1 & immig==1

  
  g freq=1
  
 
  collapse (sum) freq incumb interim  nbheur_interim sbrut_interim  indic_cdd nbheur nbheur_cdd indicmiwage s_brut sbrut_cdd indicnew (mean) w_entrant_imm w w_immig w_interim w_cdd w_entrant w_incub, by(siren_def ape)
  
  duplicates tag siren_def, g(flag)
  drop if flag>0
  rename freq freq_natives
  merge 1:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\users20172018.dta"
  g user=0
  replace user=1 if _m==3
  drop if _m==2
  g logw=log(w)
  g logsize=log(nbheur)
  reghdfe logw i.user, absorb(ape) vce(robust)
  reghdfe logw logsize i.user, absorb(ape) vce(robust)
  su w if user==1, d 
  g cdd=0
  replace cdd=1 if indic_cdd>0
  bys user: tab cdd
  g s_cdd=nbheur_cdd/nbheur
  bys user: su s_cdd, d 
  g min=0
  replace min=1 if indicmiwage>0
  
  twoway (hist w if user==1 & w<40 & w>6, width(0.1) frac color(red%30)) ///
  (hist w if user==0 & w<40 & w>6,  width(0.1) frac color(green%30)), ///
  graphregion(fcolor(white) lcolor(white)) legend( label(1 "Using Firm") label(2 "Non Using Firm") ///
  ring(0) pos(1) cols(1)) xtitle("Average Gross Hourly Wage at the Firm")
  graph export  "C:\Users\Public\Documents\Data\uservsnonusers_w_dads2018av.png", replace
  
  g etep=nbheur/1519
  
  drop _m
  rename siren_def siren
  
  *merge balance-sheets data for year 2018 
  merge 1:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta"
  g ficus=""
  replace ficus="matched" if _m==3
  replace ficus="onlyficus" if _m==2
  replace ficus="onlydads" if _m==1

  egen uniquef=group(siren) if _m==3 & user==1
  su redi_e200 if user==1, d 
  su etep if user==1, d 
  rename d310 soustraitance
   rename f001 benefice
   rename prodven prodven
   rename actinet_tot actifnet
   rename redi_e001 emp_pers
   rename redi_e200 etp
   rename redi_r216 wagebill
   rename redi_r310 canet
   g logcanet=log(canet)
   g wagef=wagebill/emp_pers

   su canet if user==1, d 

   g caratio=actifnet/prodven
   g labourratio=wagebill/prodven
   g profitworker=benefice/emp_pers
   g caworker=actifnet/emp_pers
   
   
   global varlist soustraitance benefice prodven actifnet emp_pers etp 
   
   foreach y in $varlist{
   g log`y'=log(`y')
   }
   foreach y in profitworker {
   g log`y'=log(`y')
   }
   foreach y in caworker caratio labourratio{
   g log`y'=log(`y')
   }
   
   g part1=substr(ape_diff,1,2)
   g part2=substr(ape_diff,3,5)
   g pt="."
   g B=part1+pt+part2
   drop _m
   merge m:1 B using "C:\Users\Public\Documents\Data\FICUS\naf.dta"
   
   g tag=0
   replace tag=1 if part1=="01"
   
   *Importer premium 
   reghdfe logw i.user if tag==0,absorb(ape_diff)
   reghdfe logprodven i.user if tag==0,absorb(ape_diff)
   reghdfe logsoustraitance i.user if tag==0,absorb(ape_diff)   
   reghdfe logactifnet i.user if tag==0,absorb(ape_diff)
   reghdfe logetp i.user if tag==0,absorb(ape_diff)
   reghdfe logcaratio i.user if tag==0,absorb(ape_diff)
   reghdfe loglabourratio i.user if tag==0,absorb(ape_diff)
   reghdfe logprofitworker i.user if tag==0,absorb(ape_diff)
   reghdfe logcaworker i.user if tag==0,absorb(ape_diff)
   preserve
   keep logcanet user tag ape_diff
   reghdfe logcanet i.user if tag==0,absorb(ape_diff)
   restore
   save "C:\Users\Public\Documents\Data\FICUS\ficusDADS_firm_2018.dta", replace
   
   
   
   
   
   
   
   use "C:\Users\Public\Documents\Data\FICUS\ficusDADS_firm_2018.dta", clear 
   drop if _m==2
   keep ape_diff ape siren nbheur* etp freq user w* freq_natives ficus numid
   *merge with info on average wage and hours worked by psoted workers 
   rename siren siren_def
   *merge with info on posting use in 2018 to flag firms using posting in 2018
   merge 1:1 siren_def using  "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpdcollapsed.dta"
   drop if _m==2
   count if _m==3 & ficus=="matched"
   count if _m==3 & w!=.
   
   g logw=log(w)
   g logsalaire_r=log(salaire_r)
   
   
   twoway (hist salaire_r if user==1 & salaire_r<40 & salaire_r>6, width(0.1) freq color(red%30)) ///
  (hist w if user==1 & w<40 & w>6,  width(0.1) freq color(green%30)), ///
  graphregion(fcolor(white) lcolor(white)) legend( label(1 "Posted Workers Wage") label(2 "Domestic Workers Wage") ///
  ring(0) pos(1) cols(1)) xtitle("Average Gross Hourly Wage in Using Firms")
  graph export  "C:\Users\Public\Documents\Data\FICUS\avwage_pwvsdomestic2018.png", replace
 
  g logwage=log(w)
  g logwagecdd=log(w_cdd)
  g logwagpw=log(salaire_r)
  g lognew=log(w_entrant)
  g logincub=log(w_incub)
  g loginterim=log(w_interim)
  g logimmig=log(w_immig)
  g logimmignew=log(w_entrant_imm)
  
  *Figure 
  binscatter w salaire_r if salaire_r<80 & salaire_r>6 & w>6 & user==1, nq(50) reportreg ///
  graphregion(fcolor(white) lcolor(white)) xtitle("Posted Workers Wage (Euros)") ytitle("Domestic Workers Wage (Euros)")
  graph export  "C:\Users\Public\Documents\Data\FICUS\binscatterwage_2018.png", replace

  
   
  
   twoway (hist salaire_r if user==1 & salaire_r<40 & salaire_r>6, width(0.1) freq color(red%30)) ///
  (hist w_entrant if user==1 & w_entrant<40 & w_entrant>6,  width(0.1) freq color(blue%30)) ///
  (hist w_incub if user==1 & w_incub<40 & w_incub>6,  width(0.1) freq color(green%30)), ///
  graphregion(fcolor(white) lcolor(white)) legend( label(1 "Posted Workers ") label(3 "In-House Incumbent Workers ") ///
  label(2 "Newly Hired In-House Workers") ring(0) pos(1) cols(1)) xtitle("Average Gross Hourly Wage in Using Firms")
  graph export  "C:\Users\Public\Documents\Data\FICUS\avwage_pwvsdomesticnew_2018.png", replace
  
  


	
	reghdfe logwagpw logincub if salaire_r<80 & salaire_r>6 & w_incub>6 & user==1, absorb(ape)
	global coef=round(_b[logincub],.01)
	  binscatter logwagpw logincub if salaire_r<80 & salaire_r>6 & w_incub>6 & user==1, absorb(ape) nq(50) reportreg ///
  graphregion(fcolor(white) lcolor(white)) ytitle("Posted Workers Wage (Log)") xtitle("Incumbents Wage At Using Firm (Log)") ///
  ylabel(2 (1) 4) xlabel(2 (1) 4) ttext(2 3.5 "Correlation=$coef")
  graph addplot line logincub logincub if salaire_r<80 & salaire_r>6 & w_incub>6 & user==1 & logincub<4 & logwagpw<4
    graph export  "C:\Users\Public\Documents\Data\FICUS\binscatterwagelog_2018.png", replace

 
 
	reghdfe logwagecdd logincub if w_cdd<80 & w_cdd>6 & w>6 & user==1, absorb(ape)
	global coef=round(_b[logincub],.01)
	  binscatter logwagecdd logincub  if w_cdd<80 & w_cdd>6 & w_incub>6 & user==1, absorb(ape) nq(50) reportreg ///
  graphregion(fcolor(white) lcolor(white)) ytitle("In-House Short-Term Workers Wage (Log)") xtitle("Incumbents Wage At Using Firm (Log)") ///
  ylabel(2 (1) 4) xlabel(2 (1) 4) ttext(2 3.5 "Correlation=$coef")
  graph addplot line logincub logincub if salaire_r<80 & salaire_r>6 & w_cdd>6 & user==1 & logwagecdd<4 & logincub<4
    graph export  "C:\Users\Public\Documents\Data\FICUS\binscatterwagecddlog_2018.png", replace
	
	reghdfe logimmig logincub if w_immig<80 & w_immig>6 & w>6 & user==1, absorb(ape)
	global coef=round(_b[logincub],.001)
	  binscatter logimmig logincub  if w_cdd<80 & w_cdd>6 & w_incub>6 & user==1, absorb(ape) nq(50) reportreg ///
  graphregion(fcolor(white) lcolor(white)) ytitle("In-House Foreign Workers Wage (Log)") xtitle("Incumbents Wage At Using Firm (Log)") ///
  ylabel(2 (1) 4) xlabel(2 (1) 4) ttext(2 3.5 "Correlation=.69")
  graph addplot line logincub logincub if salaire_r<80 & salaire_r>6 & w_cdd>6 & user==1 & logwagecdd<4 & logincub<4
    graph export  "C:\Users\Public\Documents\Data\FICUS\binscatterwageimmiglog_2018.png", replace
	
	
	*Figure D.LIV
	
	reghdfe logimmignew logincub if w_immig<80 & w_immig>6 & w>6 & user==1, absorb(ape)
	global coef=round(_b[logincub],.001)
	 binscatter logimmignew logincub  if w_cdd<80 & w_cdd>6 & w_incub>6 & user==1, absorb(ape) nq(50) reportreg ///
  graphregion(fcolor(white) lcolor(white)) ytitle("In-House Newly Hired Foreign Workers Wage (Log)") xtitle("Incumbents Wage At Using Firm (Log)") ///
  ylabel(2 (1) 4) xlabel(2 (1) 4) ttext(2 3.5 "Correlation=$coef")
  graph addplot line logincub logincub if salaire_r<80 & salaire_r>6 & w_cdd>6 & user==1 & logwagecdd<4 & logincub<4
    graph export  "C:\Users\Public\Documents\Data\FICUS\binscatterwageimmignewlog_2018.png", replace
	
	
	reghdfe  lognew logincub if w_entrant<80 & w_entrant>6 & w_incub>6 & user==1, absorb(ape)
	global coef=round(_b[logincub],.01)
	  binscatter  lognew logincub if w_entrant<80 & w_entrant>6 & w_incub>6 & user==1, nq(50) absorb(ape) reportreg ///
  graphregion(fcolor(white) lcolor(white)) ytitle("In-House Newly Hired Workers Wage (Log)") xtitle("Incumbents Wage At Using Firm (Log)") ///
  ylabel(2 (1) 4) xlabel(2 (1) 4) ttext(2 3.5 "Correlation=$coef")
  graph addplot line logincub logincub if salaire_r<80 & salaire_r>6 & w_entrant>6 & user==1 & logincub<4 & lognew<4
    graph export  "C:\Users\Public\Documents\Data\FICUS\binscatterwagenewlog_2018.png", replace

	preserve 
	
	keep if user==1
	
	keep lognew logincub logwagpw logwagecdd loginterim siren ape freq
	rename logincub incub
	reshape long log, i(siren incub ape freq) j(type) string
	

	binscatter  log incub, by(type) absorb(ape) nq(30) reportreg ///
	legend(label(1 "Newly Hired Workers" ) label(2 "Fixed-Term Workers" ) label(3 "Posted Workers")) ///
	xtitle("In-House Incumbent Wages At Using Firm (Log)") ytitle("Non Incumbent Wage (Log)")
    graph export  "C:\Users\Public\Documents\Data\FICUS\correlation_incumbentothers_2018.png", replace
    
	reghdfe log incub if type=="wagpw" , absorb(ape)
	global bpw=round(_b[incub],.01)
	global sepw=round(_se[incub],.01)
	  
	reghdfe log incub if type=="new", absorb(ape)
	global bnew=round(_b[incub],.01)
	global senew=round(_se[incub],.01)

	reghdfe log incub if type=="interim", absorb(ape)
    global binterim=round(_b[incub],.01)
	global seinterim=round(_se[incub],.01)
	
	egen group=group(type)
	g order=1 if type=="new"
	replace order=2 if type=="wagpw"
	replace order=3 if type=="interim"
	
	*FIGURE 7, panel A
	binscatter  log incub if type!="wagecdd" & type!="interim" , by(order) absorb(ape) nq(50) reportreg ///
	msymbols( o t) ///
	legend(label(1 "Newly Hired Workers" )  label(2 "Posted Workers") order(1 2) cols(1) ring(0) pos(11)) ///
	xtitle("In-House Incumbent Wages At Using Firm (Log)") ytitle("Non Incumbent Wage (Log)") ylabel(2 (1) 4) xlabel(2 (1) 4) ///
	ttext( 3.6 3.7 "Correlation=$bnew($senew)", place(e) size(small)) ///
	ttext( 3.0 3.7 "Correlation=$bpw($sepw)", place(e) size(small)) 
    graph export  "C:\Users\Public\Documents\Data\FICUS\correlation_incumbentothers2_2018.png", replace

	reg log incub if type=="wagpw" 
	global bpw=round(_b[incub],.01)
	global sepw=round(_se[incub],.01)
	  
	reg log incub if type=="new"
	global bnew=round(_b[incub],.01)
	global senew=round(_se[incub],.01)

	reg log incub if type=="interim"
    global binterim=round(_b[incub],.01)
	global seinterim=round(_se[incub],.01)
	
    *Figure D.L.III, panel B
	
	binscatter  log incub if type!="wagecdd" & type!="interim" , by(order)  nq(50) reportreg ///
	msymbols( o t) ///
	legend(label(1 "Newly Hired Workers" )  label(2 "Posted Workers") order(1 2) cols(1) ring(0) pos(11)) ///
	xtitle("In-House Incumbent Wages At Using Firm (Log)") ytitle("Non Incumbent Wage (Log)") ylabel(2 (1) 4) xlabel(2 (1) 4) ///
	ttext( 3.9 3.7 "Correlation=$bnew($senew)", place(e) size(small)) ///
	ttext( 2.8 3.7 "Correlation=$bpw($sepw)", place(e) size(small)) 
    graph export  "C:\Users\Public\Documents\Data\FICUS\correlation_incumbentothers2noape_2018.png", replace

	
	reghdfe log incub if type=="wagpw" , absorb(ape)
	global bpw=round(_b[incub],.01)
	global sepw=round(_se[incub],.01)
	  
	reghdfe log incub if type=="new", absorb(ape)
	global bnew=round(_b[incub],.01)
	global senew=round(_se[incub],.01)

	reghdfe log incub if type=="interim", absorb(ape)
    global binterim=round(_b[incub],.01)
	global seinterim=round(_se[incub],.01)
	
	*Figure D.L.III, panel A
	binscatter  log incub if type!="wagecdd" , by(order) absorb(ape) nq(50) reportreg ///
	msymbols( o t d) ///
	legend(label(1 "Newly Hired Workers" )  label(2 "Posted Workers") label(3 "Temp Workers") order(1 2 3) cols(1) ring(0) pos(11)) ///
	xtitle("In-House Incumbent Wages At Using Firm (Log)") ytitle("Non Incumbent Wage (Log)") ylabel(2 (1) 4) xlabel(2 (1) 4) ///
	ttext( 3.6 3.7 "Correlation=$bnew($senew)", place(e) size(small)) ///
	ttext( 2.7 3.7 "Correlation=$binterim($seinterim)", place(e) size(small))  ///
    ttext( 3.1 3.7 "Correlation=$bpw($sepw)", place(e) size(small))
    graph export  "C:\Users\Public\Documents\Data\FICUS\correlation_incumbentothersinterim_2018.png", replace

 
 	restore
	
	
	g tx=nbheur_posted/nbheur
	drop _m
	rename ape B 
    merge m:1 B using "C:\Users\Public\Documents\Data\FICUS\naf.dta"
    drop _m 
    tab E if user==1 [aweight=nbheur_posted], sort
    tab ape if user==1 [aweight=nbheur_posted], sort
	
	g indic_interim=0
	replace indic_interim=1 if nbheur_interim>0
    replace indic_interim=0 if missing(nbheur_interim)
	
	
	
	*********ADDITIONAL DESCRIPTIVES STATISTICS******************
	
	preserve 
	drop if ficus=="onlyficus"
	g wagebill_dads=w*nbheur
	collapse (sum) nbheur freq_natives etp wagebill wagebill_dads, by(user)
	foreach y in  nbheur freq_natives etp wagebill wagebill_dads{
	egen tot`y'=sum(`y')
	g s`y'=`y'/tot`y'
	}
	restore 
	*****
	
   use "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpd.dta", clear
   g pw_wagebill=salaire_r*duration*10
   g count=1
   g siren_def="."
   replace siren_def=siren1 if status2=="step1"
   replace siren_def=siren2 if status2=="step2"
   replace siren_def=siren3 if status2=="step3"
   replace siren_def=siren if status2=="step4"
   g do_name="."
   replace do_name=name if status2=="step4"
   replace do_name=old_denom if status2=="step1"
   replace do_name=old_denom if status2=="step2"
   replace do_name=old_denom if status2=="step3"

  preserve 
  drop if siren_def=="."
  collapse (sum) pw_wagebill count (mean) redi_e001 redi_e200  redi_r216, by(siren_def ape_do do_name)
  g s_money=pw_wagebill/(redi_r216*1000)
  g s_people= redi_e001/count
  
    collapse (sum) pw_wagebill count redi_e001 redi_e200  redi_r216
restore 

  drop if siren_def=="."
  *nombre de clients par entreprise
  collapse (sum) count pw_wagebill, by(adrent_pays identifiant_entreprise siren_def)
  g freq=1
  egen client=sum(freq), by(siren_def)
  
  collapse (mean) client (sum) count pw_wagebill, by(siren_def adrent_pays)
  g pays=1
  collapse (sum) pays count pw_wagebill (mean) client , by(siren_def)
  su client, d 
  rename siren_def siren
  merge 1:1 siren using "C:\Users\Public\Documents\Data\FICUS\ficus2018.dta"
  g user=1 if _m==3
  replace pays=0 if user==. 
  g logca=log(prodven)
  g logcount=log(count)
  replace user=0 if user==. 
  g part1=substr(ape_diff,1,2)
  g tag=0
  replace tag=1 if part1=="01"
  reghdfe logca i.user if tag==0,absorb(ape_diff)

  reghdfe logca i.pays if tag==0, absorb(ape_diff)
  
  *Figure C18 
  coefplot, keep(1.pays 2.pays 3.pays 4.pays 5.pays 6.pays 7.pays ///
  8.pays 9.pays 10.pays 11.pays 12.pays) vertical graphregion(fcolor(white) lcolor(white)) ///
  ytitle("Premium") xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10" 11 "11" 12 "12"  ) ///
  xtitle("Number of country from which firm sources posting services") ylabel(0 (2) 10)
   graph export  "C:\Users\Public\Documents\Data\FICUS\sizepremia.png", replace
   
   replace count=0 if count==. 
   
    reghdfe logca i.pays count if tag==0, absorb(ape_diff)
  coefplot, keep(1.pays 2.pays 3.pays 4.pays 5.pays 6.pays 7.pays ///
  8.pays 9.pays 10.pays 11.pays 12.pays) vertical graphregion(fcolor(white) lcolor(white)) ///
  ytitle("Premium") xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10" 11 "11" 12 "12"  ) ///
  xtitle("Number of country from which firm sources posting services") ylabel(0 (2) 6)
   graph export  "C:\Users\Public\Documents\Data\FICUS\sizepremiacontrol.png", replace

   
   g freqi=1 
   rename redi_e001 emp_pers
   
   g wager1= redi_r216/emp_pers
   g wager2= redi_r216/redi_e200

   preserve 
   
  collapse (sum) freqi emp_pers prodven count pw_wagebill redi_r216, by(user a21)

   drop if a21=="A"
   egen totposting=sum(freqi), by(user)
   g s=freqi/totposting
   g label="."
   replace label="Extractive Industries" if a21=="B"
   replace label="Manufacturing" if a21=="C"
   replace label="Electricity Supply" if a21=="D"
   replace label="Water Supply" if a21=="E"
   replace label="Construction" if a21=="F"
   replace label="Repair/Retail of Vehicles"  if a21=="G"
   replace label="Transport"  if a21=="H"
   replace label="Hotels Restaurants"  if a21=="I"
   replace label="ICT"  if a21=="J"
   replace label="Finance and Insurance"  if a21=="K"
   replace label="Real estate"  if a21=="L"
   replace label="Business Support"  if a21=="M"
   replace label="Scientific and Technical"  if a21=="N"
   replace label="Public Administration"  if a21=="O"
   replace label="Teaching"  if a21=="P"
   replace label="Health"  if a21=="Q"
   replace label="Arts"  if a21=="R"
   replace label="Households"  if a21=="S"
   replace label="Extra"  if a21=="T"
  g order=-s
   graph bar s if user==1, over(label, label(angle(45)) sort(order))  ///
   graphregion(fcolor(white) lcolor(white)) ytitle("") ///
   ylabel(0.1 "10%" 0.2"20%")
   graph export  "C:\Users\Public\Documents\Data\FICUS\distrib_firms_a21.png", replace
   
   egen totemp=sum(emp_pers), by(a21)
   g s_emp=emp_pers/totemp
   g order2=-s_emp
   graph bar s_emp if user==1, over(label, label(angle(45)) sort(order2))  ///
   graphregion(fcolor(white) lcolor(white)) ytitle("") ///
   ylabel(0.1 "10%" 0.2"20%" 0.3"30%" 0.4"40%")
  graph export  "C:\Users\Public\Documents\Data\FICUS\distrib_employees_a21.png", replace
  
   
   egen totpost=sum(count), by(user)
   g s_post=count/totpost
   g order3=-s_post
   graph bar s_post if user==1, over(label, label(angle(45)) sort(order3))  ///
   graphregion(fcolor(white) lcolor(white)) ytitle("") ///
   ylabel(0.1 "10%" 0.2"20%" 0.3"30%" 0.4"40%")
  graph export  "C:\Users\Public\Documents\Data\FICUS\distrib_postedworkers_a21.png", replace
   
   
    egen totbill=sum(pw_wagebill), by(user)
   g s_bill=pw_wagebill/totbill
   g order4=-s_bill
   graph bar s_bill if user==1, over(label, label(angle(45)) sort(order4))  ///
   graphregion(fcolor(white) lcolor(white)) ytitle("") ///
   ylabel(0.1 "10%" 0.2"20%" 0.3"30%" 0.4"40%")
  graph export  "C:\Users\Public\Documents\Data\FICUS\distrib_pwwagebill_a21.png", replace
   
   replace redi_r216=redi_r216*1000
   egen totwagebill=sum(redi_r216), by(a21)
   g s_wbill=pw_wagebill/totwagebill  
   g order5=-s_wbill
   graph bar s_wbill if user==1, over(label, label(angle(45)) sort(order5))  ///
   graphregion(fcolor(white) lcolor(white)) ytitle("") 
  

   
   restore 
   
   
   
   
************************************************
* Figure 7b 
***********************************************

   *Dataset for year 2018
   use "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpd.dta", clear
   g pw_wagebill=salaire_r*duration*10
   g count=1
   g siren_def="."
   replace siren_def=siren1 if status2=="step1"
   replace siren_def=siren2 if status2=="step2"
   replace siren_def=siren3 if status2=="step3"
   replace siren_def=siren if status2=="step4"
   replace siren_def=siren_old if status2=="step5"
   g do_name="."
   replace do_name=name if status2=="step4"
   replace do_name=old_denom if status2=="step1"
   replace do_name=old_denom if status2=="step2"
   replace do_name=old_denom if status2=="step3"
   drop if siren_def=="."
   keep siren_def salaire_r age sex duration hash_salarie identifiant_entreprise ape_do metier
   compress 
   replace duration=duration*10
   collapse (mean) salaire_r age duration sex, by(hash_salarie siren_def)
   g year=2018
   g posted=1
   save "C:\Users\Public\Documents\Data\ESA\sal2018formatch2.dta", replace
  
   *Dataset for year 2017
   use "C:\Users\Public\Documents\Data\SIPSI\matchedfaredpd2017.dta", clear
   g pw_wagebill=salaire_r*duration*10
   g count=1
   g siren_def="."
   replace siren_def=siren1 if status2=="step1"
   replace siren_def=siren2 if status2=="step2"
   replace siren_def=siren3 if status2=="step3"
   replace siren_def=siren if status2=="step4"
   replace siren_def=siren_old if status2=="step5"
   g do_name="."
   replace do_name=name if status2=="step4"
   replace do_name=old_denom if status2=="step1"
   replace do_name=old_denom if status2=="step2"
   replace do_name=old_denom if status2=="step3"
   drop if siren_def=="."
   keep siren_def salaire_r age sex duration hash_salarie identifiant_entreprise ape_do metier
   compress 
   replace duration=duration*10
   collapse (mean) salaire_r age duration sex, by(hash_salarie siren_def)
   g year=2017
   g posted=1
   save "C:\Users\Public\Documents\Data\ESA\sal2017formatch2.dta", replace
  
   *DADS for year 2018: one job spell by french employee
   use "C:\Users\Public\Documents\Data\ESA\dads2018.dta", clear 
   append using "C:\Users\Public\Documents\Data\ESA\dads20182.dta"
   append using "C:\Users\Public\Documents\Data\ESA\dads20183.dta"
   append using "C:\Users\Public\Documents\Data\ESA\dads20184.dta"
   drop if filt=="2"
   drop if filt_1=="2"
   g interim_0=1
   replace interim_0=0 if missing(siret_utilisateur)
   g siren_all_0=siren_empl
   replace siren_all_0="." if interim_0==1
   gen siren_interim_0=substr(siret_utilisateur,1,9)
   replace siren_all_0=siren_interim_0 if interim_0==1
   g interim_1=1
   replace interim_1=0 if missing(siret_utilisateur_1)
   g siren_all_1=siren_empl_1
   replace siren_all_1="." if interim_1==1
   gen siren_interim_1=substr(siret_utilisateur_1,1,9)
   replace siren_all_1=siren_interim_1 if interim_1==1
   
   rename siren_all_1 siren_def
   merge m:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\users20172018.dta"
   g user=0
   replace user=1 if _m==3
   drop if _m==2
  * drop salaire_r pw_wagebill pw_count ape_do _merge
   foreach var in  user{
   rename `var' `var'_1
   }
   rename siren_def siren_all_1
   rename siren_all_0 siren_def
   drop _m
   merge m:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\users20172018.dta"
   g user=0
   replace user=1 if _m==3   
   drop if _m==2
   *drop salaire_r pw_wagebill pw_count ape_do _merge
   foreach var in  user{
   rename `var' `var'_0
   }
   drop siren
   drop siret 
   rename siren_def siren_all_0
   drop _m
   global varlist   apen contrat_travail filt nbheur s_brut siret_utilisateur siren_empl 
   foreach y in $varlist{
   rename `y' `y'_0
   }
   g age_1=age-1
   rename age age_0
   rename sexe sexe_0
   gen sexe_1=sexe_0
   
   
   gen select=user_0+user_1
   egen t1=max(user_0), by(siren_all_0) 
   egen t2=max(user_1), by(siren_all_1) 
   g bis=t1+t2
   *keep domestic job spells at users (matched with panel of users)
   keep if select>0
   drop t1 t2 select
   drop bis
   set type double
   g jobid=_n
   *use "-1" variables to create 2017-2018 panel
   reshape long apen contrat_travail filt user interim nbheur siren_all s_brut siret_utilisateur sexe siren_empl age siren_interim, i(ident_s jobid) j(period) string
   g year=2017 if period=="_1"
   replace year=2018 if period=="_0"
  save "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustifusers20172018.dta", replace
  
  
   use "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustifusers20172018.dta", clear
   *hourly wage for domestic workers 
   g w=s_brut/nbheur
   g logwage=log(w)
   destring sex, replace
   rename siren_all siren_def
   rename nbheur duration
   append using "C:\Users\Public\Documents\Data\ESA\sal2018formatch.dta"
   replace posted=0 if missing(posted) & year==2018 
   replace interim=0 if posted==1	
   *getting the same identifier variable name for posted and domestic workers 
   *missing(apend) for posted workers only
   tostring ident_s, replace
   replace ident_s=hash_salarie if missing(apen)
   append using "C:\Users\Public\Documents\Data\ESA\sal2017formatch.dta"
   replace posted=0 if missing(posted) & year==2017 
   replace ident_s=hash_salarie if missing(apen) & year==2017
   replace interim=0 if posted==1
   drop logwage  
   
   g logwage=log(w) if posted==0 
   replace logwage=log(salaire_r) if posted==1
   
   replace interim=0 if posted==1
   g age3=age*age
   g age2=age*age
   g logduration=log(duration)
   egen test=sum(posted), by( siren_def )
   drop if test==0 
  
  ********************************************
  * Wage penalty (Table 2, panel B)
  *********************************************
  
  
 reghdfe logwage i.posted, absorb(siren_def year) cl(ident_s)
 outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", replace
 reghdfe logwage i.posted duration, absorb(siren_def year) cl(ident_s)
 outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append
 reghdfe logwage i.posted logduration, absorb(siren_def year) cl(ident_s)
 outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append
 reghdfe logwage i.posted i.interim, absorb(siren_def year) cl(ident_s)  
  outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append
 reghdfe logwage i.posted i.interim, absorb(siren_def year age) cl(ident_s)  
  outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append
 reghdfe logwage i.posted i.interim duration age age2 age3, absorb(siren_def year) cl(ident_s )  
 outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append
 reghdfe logwage i.posted i.interim logduration age age2 age3, absorb(siren_def year) cl(ident_s )  
 outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append

 
  g immig=0
  replace immig=1 if dep_naiss=="99"
  
  reghdfe logwage i.posted i.immig logduration age age2 age3, absorb(siren_def year ) cl(ident_s )  
  outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append
  reghdfe logwage i.posted i.immig i.interim logduration age age2 age3, absorb(siren_def year ) cl(ident_s )  
  outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append
  
  g sample=0
  replace sample=1 if posted==0 & immig==1
  replace sample=1 if posted==1

  reghdfe logwage i.posted logduration age age2 age3 if sample==1, absorb(siren_def year ) cl(ident_s )  
  outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append
  
  reghdfe logwage i.immig logduration age age2 age3 if posted==0, absorb(siren_def year ) cl(ident_s )  
  outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\penalty.tex", append

  egen numsiren=group(siren_def) 
  
  *estimating firm fixed effects by posted status 
  reghdfe logwage  duration age age2 age3, absorb(fe=numsiren##posted year ident_s) cl(ident_s)  
  
  reghdfe logwage duration age age2 age3 if sample==1, absorb(fe4=numsiren##posted year ident_s ) cl(ident_s )  
  
  save "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustifusers20172018_fe.dta", replace

   
  use "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustifusers20172018_fe.dta", clear
  * reghdfe logwage  duration age3 if posted==1, absorb(fe_posted=numsiren year ident_s sexe) cl(ident_s )  
  *  reghdfe logwage  duration age3 if posted==0, absorb(fe_nonposted=numsiren year ident_s sexe) cl(ident_s )  
   *save "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustifusers20172018_fe2.dta", replace

   g tagi=1
   collapse (mean) fe fe4 salaire_r (sum) tagi duration , by(numsiren posted)
 
 reshape wide fe fe4 /*fe_posted fe_nonposted*/ salaire_r duration tagi, i(numsiren) j(posted)
 su fe0 if fe0<1 & fe0>-1.5, d 
 global mean0=round(r(mean),.01)
 su fe1 if fe1<1 & fe1>-1.5, d 
 global mean1=round(r(mean),.01)
 
 
 *Fig DL.II, Panel B 
 
 
 twoway (hist fe0 if fe0<1.5 & fe0>-1.5, frac color(blue%30) width(0.02)) ///
 (hist fe1 if fe1<1.5 & fe1>-1.5, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Domestic Workers") label(2 "Posted Workers") cols(1) ring(0) pos(1)) ///
 xline($mean0, lcolor(blue) lpattern(dash))  xline($mean1, lcolor(red) lpattern(dash)) ///
 xtitle("Firm FE") xlabel(-1.5 (0.5) 1.5)
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFE_byposting.pdf", replace

 
 twoway (hist fe0 if fe0<1.5 & fe0>-1.5, frac color(blue%30) width(0.02)) ///
 (hist fe1 if fe1<1.5 & fe1>-1.5, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Domestic Workers") label(2 "Posted Workers") cols(1) ring(0) pos(1)) ///
 xline($mean0, lcolor(blue) lpattern(dash))  xline($mean1, lcolor(red) lpattern(dash)) ///
 xtitle("Firm FE") xlabel(-1.5 (0.5) 1.5) ///
  ttext(.045 0.3 "Mean Domestic Workers= $mean0", place(e) ) ///
ttext(.04 0.3 "Mean Posted Workers = $mean1", place(e)) 
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFE_byposting_mean.pdf", replace


 g logwagpw=log(salaire_r1) 
 
 keep if fe1!=. & fe0!=.
 xtile fe1r=fe1, nq(20)
 xtile fe0r=fe0, nq(20)
 
 g fe1_n=fe1
 g fe0_n=fe0
 replace fe1_n=0 if fe1r==1
 replace fe0_n=0 if fe0r==1
 
 *normalize 
 
 foreach y in 0 1{
 su fe`y' if fe`y'r==1
 g fe`y'_norm=fe`y'-r(mean)
 }

 su fe0 if fe0r==1
 foreach y in 0 1{
 g fe`y'_norm2=fe`y'-r(mean)
 }
 
 
 su fe0 if fe0<1.5 & fe0>-1.5
 foreach y in 0 1{
 g fe`y'_norm3=fe`y'-r(mean)
 }
 
 
 su fe0 if fe0<1 & fe0>-1.5, d 
 global mean0=round(r(mean),.01)
 su fe1 if fe1<1 & fe1>-1.5, d 
 global mean1=round(r(mean),.01)
 
 twoway (hist fe0 if fe0<1.5 & fe0>-1.5, frac color(blue%30) width(0.02)) ///
 (hist fe1 if fe1<1.5 & fe1>-1.5, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Domestic Workers") label(2 "Posted Workers") cols(1) ring(0) pos(1)) ///
 xline($mean0, lcolor(blue) lpattern(dash))  xline($mean1, lcolor(red) lpattern(dash)) ///
 xtitle("Firm FE") xlabel(-1.5 (0.5) 1.5)
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFE_byposting_nonmiss.pdf", replace

 
 
 twoway (hist fe0 if fe0<1.5 & fe0>-1.5, frac color(blue%30) width(0.02)) ///
 (hist fe1 if fe1<1.5 & fe1>-1.5, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Domestic Workers") label(2 "Posted Workers") cols(1) ring(0) pos(1)) ///
 xline($mean0, lcolor(blue) lpattern(dash))  xline($mean1, lcolor(red) lpattern(dash)) ///
 xtitle("Firm FE") xlabel(-1.5 (0.5) 1.5) ///
  ttext(.045 0.3 "Mean Domestic Workers= $mean0", place(e) ) ///
ttext(.04 0.3 "Mean Posted Workers = $mean1", place(e)) 
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFE_byposting_meannonmiss.pdf", replace

 
 
 reg fe1 fe0
 global beta=round(_b[fe0],.01)
 global se=round(_se[fe0],.01)
 
 binscatter fe1 fe0 , ylabel(-1 (0.5) 1.5) xlabel(-1 (0.5) 1.5) nq(100) reportreg ///
 graphregion(fcolor(white) lcolor(white)) xtitle("Domestic Workers Firm FE") ytitle("Posted Workers Firm FE") ///
 ttext(1.5 -1 "Slope=$beta($se)", place(e))
 graph addplot line fe1 fe1 if fe1>-1 & fe1<1.5
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFEcorr.pdf", replace
 
  binscatter fe1 fe0 , ylabel(-0.8 (0.4) 0.8) xlabel(-0.8 (0.4) 0.8) nq(20) reportreg ///
  graphregion(fcolor(white) lcolor(white)) xtitle("Domestic Workers Firm FE") ytitle("Posted Workers Firm FE") ///
 ttext(0.8 -0.8 "Slope=$beta($se)", place(e))
 graph addplot line fe1 fe1 if fe1>-0.8 & fe1<0.8
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFEcorr_20bin.pdf", replace
 

  ********************************************
  * Figure 7b 
  *********************************************
  
  binscatter fe1_norm2 fe0_norm2, ylabel(0 (0.5) 1.5) xlabel(0 (0.5) 1.5) nq(20) reportreg ///
  graphregion(fcolor(white) lcolor(white)) xtitle("Domestic Workers Firm FE") ytitle("Posted Workers Firm FE") ///
  ttext(1.5 0 "Slope=$beta($se)", place(e))

 graph addplot line fe1_norm2 fe1_norm2 if fe1_norm2>0 & fe1_norm2<1.5
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFEcorr2.pdf", replace
 
 su fe0_norm3 if fe0<1 & fe0>-1.5, d 
 global mean0=round(r(mean),.01)
 su fe1_norm3 if fe1<1 & fe1>-1.5, d 
 global mean1=round(r(mean),.01)
 
 twoway (hist fe0_norm3 if fe0<1.5 & fe0>-1.5, frac color(blue%30) width(0.02)) ///
 (hist fe1_norm3 if fe1<1.5 & fe1>-1.5, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Domestic Workers") label(2 "Posted Workers") cols(1) ring(0) pos(1)) ///
 xline($mean0, lcolor(blue) lpattern(dash))  xline($mean1, lcolor(red) lpattern(dash)) ///
 xtitle("Firm FE") xlabel(-1.5 (0.5) 1.5) ///
  ttext(.045 0.3 "Mean Domestic Workers= $mean0", place(e) ) ///
ttext(.04 0.3 "Mean Posted Workers = $mean1", place(e)) 

 
 
 
 
  ********************************************
  * Wage premium of users (Table 2, Panel A)
  ********************************************
 
 
 
   use "C:\Users\Public\Documents\Data\ESA\dads2018.dta", clear 
   append using "C:\Users\Public\Documents\Data\ESA\dads20182.dta"
   append using "C:\Users\Public\Documents\Data\ESA\dads20183.dta"
   append using "C:\Users\Public\Documents\Data\ESA\dads20184.dta"
   drop if filt=="2"
   drop if filt_1=="2"
   g interim_0=1
   replace interim_0=0 if missing(siret_utilisateur)
   g siren_all_0=siren_empl
   replace siren_all_0="." if interim_0==1
   gen siren_interim_0=substr(siret_utilisateur,1,9)
   replace siren_all_0=siren_interim_0 if interim_0==1
   
   g interim_1=1
   replace interim_1=0 if missing(siret_utilisateur_1)
   g siren_all_1=siren_empl_1
   replace siren_all_1="." if interim_1==1
   gen siren_interim_1=substr(siret_utilisateur_1,1,9)
   replace siren_all_1=siren_interim_1 if interim_1==1
   
   rename siren_all_1 siren_def
   merge m:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\users20172018.dta"
   g user=0
   replace user=1 if _m==3
   drop if _m==2
  * drop salaire_r pw_wagebill pw_count ape_do _merge
   foreach var in  user{
   rename `var' `var'_1
   }
   rename siren_def siren_all_1
   rename siren_all_0 siren_def
   drop _m
   merge m:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\users20172018.dta"
   g user=0
   replace user=1 if _m==3   
   drop if _m==2
   *drop salaire_r pw_wagebill pw_count ape_do _merge
   foreach var in  user{
   rename `var' `var'_0
   }
   drop siren
   drop siret 
   rename siren_def siren_all_0
   drop _m
   global varlist   apen contrat_travail filt nbheur s_brut siret_utilisateur siren_empl 
   foreach y in $varlist{
   rename `y' `y'_0
   }
   g age_1=age-1
   rename age age_0
   rename sexe sexe_0
   gen sexe_1=sexe_0   
   gen select=user_0+user_1
   egen t1=max(user_0), by(siren_all_0) 
   egen t2=max(user_1), by(siren_all_1) 
   g bis=t1+t2
   drop if select>0
   g freq=1
   collapse (sum) freq, by(siren_all_0)
   sample 15
   rename siren_all_0 siren_def 
   save "C:\Users\Public\Documents\Data\SIPSI\nonusers20172018.dta", replace
   *firms that are never observed using posted workers during the 2017-2018 period 
   
   
   
   use "C:\Users\Public\Documents\Data\ESA\dads2018.dta", clear 
   append using "C:\Users\Public\Documents\Data\ESA\dads20182.dta"
   append using "C:\Users\Public\Documents\Data\ESA\dads20183.dta"
   append using "C:\Users\Public\Documents\Data\ESA\dads20184.dta"
   drop if filt=="2"
   drop if filt_1=="2"
   
   
   g interim_0=1
   replace interim_0=0 if missing(siret_utilisateur)
   g siren_all_0=siren_empl
   replace siren_all_0="." if interim_0==1
   gen siren_interim_0=substr(siret_utilisateur,1,9)
   replace siren_all_0=siren_interim_0 if interim_0==1
   
   g interim_1=1
   replace interim_1=0 if missing(siret_utilisateur_1)
   g siren_all_1=siren_empl_1
   replace siren_all_1="." if interim_1==1
   gen siren_interim_1=substr(siret_utilisateur_1,1,9)
   replace siren_all_1=siren_interim_1 if interim_1==1
   
   rename siren_all_1 siren_def
   merge m:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\nonusers20172018.dta"
   g user=0
   replace user=1 if _m==3
   drop if _m==2
  * drop salaire_r pw_wagebill pw_count ape_do _merge
   foreach var in  user{
   rename `var' `var'_1
   }
   rename siren_def siren_all_1
   rename siren_all_0 siren_def
   drop _m
   merge m:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\nonusers20172018.dta"
   g user=0
   replace user=1 if _m==3   
   drop if _m==2
   *drop salaire_r pw_wagebill pw_count ape_do _merge
   foreach var in  user{
   rename `var' `var'_0
   }
   drop siren
   drop siret 
   rename siren_def siren_all_0
   drop _m
   global varlist   apen contrat_travail filt nbheur s_brut siret_utilisateur siren_empl 
   foreach y in $varlist{
   rename `y' `y'_0
   }
   g age_1=age-1
   rename age age_0
   rename sexe sexe_0
   gen sexe_1=sexe_0   
   gen select=user_0+user_1
   egen t1=max(user_0), by(siren_all_0) 
   egen t2=max(user_1), by(siren_all_1) 
   g bis=t1+t2
   *keep the non users (succesfully matched with the nonusers list above)
   keep if select>0
   drop t1 t2 select
   drop bis
   drop freq
   set type double
   g jobid=_n

   reshape long apen contrat_travail filt user interim nbheur siren_all s_brut siret_utilisateur sexe siren_empl age siren_interim, i(ident_s jobid) j(period) string
   g year=2017 if period=="_1"
  replace year=2018 if period=="_0"
 
  save "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustifnonusers20172018.dta", replace
  
  
   use "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustifnonusers20172018.dta", clear
   replace user=0 
   append using "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustifusers20172018.dta"
   g w=s_brut/nbheur
   g logwage=log(w)
   destring sex, replace
   rename siren_all siren_def
   rename nbheur duration
   g age3=age*age
   g age2=age*age
   drop user 
   merge m:1 siren_def using "C:\Users\Public\Documents\Data\SIPSI\users20172018.dta"
   drop if _m==2
   gen client=0
   replace client=1 if _m==3
   drop if siren_def=="."
   drop if missing(siren_def)
   
   reghdfe logwage i.client duration age age2 age3, absorb( year) cl(ident_s)
   reghdfe logwage i.client duration age age2 age3, absorb(ident_s year) cl(ident_s)
 

  
   egen numsiren=group(siren_def) 
   reghdfe logwage  duration age age2 age3, absorb(fe=numsiren year ident_s ) cl(ident_s )  
   save "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustif20172018_fe.dta", replace
 
   use "C:\Users\Public\Documents\Data\SIPSI\panelDADSexhaustif20172018_fe.dta", clear 
   g tagi=1
   collapse (mean) fe client (sum) duration , by(numsiren)
  
  su fe if client==0 &  fe<1 & fe>-2
  global mean0=round(r(mean),.01)
  global se0=round(r(sd),.01)
  su fe if client==1 &  fe<1 & fe>-2
  global mean1=round(r(mean),.01)
  global se1=round(r(sd),.01)
  
  
  *Figure D.L II, Panel A

twoway (hist fe if client==0 & fe<1 & fe>-2, frac color(blue%30) width(0.02)) ///
 (hist fe if client==1 & fe<1 & fe>-2, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Never Using Firms") label(2 "Ever Using Firms") cols(1) ring(0) pos(11)) ///
 xtitle("Firm FE") xline($mean0, lcolor(blue) lpattern(dash)) xline($mean1, lcolor(red) lpattern(dash))
 
  graph export  "C:\Users\Public\Documents\Data\FICUS\firmFE_byclient.pdf", replace
  
  twoway (hist fe if client==0 & fe<1 & fe>-2, frac color(blue%30) width(0.02)) ///
 (hist fe if client==1 & fe<1 & fe>-2, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Never Using Firms") label(2 "Ever Using Firms") cols(1) ring(0) pos(11)) ///
 xtitle("Firm FE") xline($mean0, lcolor(blue) lpattern(dash)) xline($mean1, lcolor(red) lpattern(dash)) ///
 ttext(.045 -2 "Mean Never Using Firms= $mean0", place(e) ) ///
ttext(.04 -2 "Mean Ever Using Firms= $mean1", place(e)) 
   graph export  "C:\Users\Public\Documents\Data\FICUS\firmFE_byclient_mean.pdf", replace

  g norm=fe-$mean0
  
  su norm if client==0 &  fe<1 & fe>-2
  global mean0n=round(r(mean),.01)
  global se0n=round(r(sd),.01)
  su norm if client==1 &  fe<1 & fe>-2
  global mean1n=round(r(mean),.01)
  global se1n=round(r(sd),.01)

  
  twoway (hist norm if client==0 & fe<1 & fe>-2, frac color(blue%30) width(0.02)) ///
 (hist norm if client==1 & fe<1 & fe>-2, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Never Using Firms") label(2 "Ever Using Firms") cols(1) ring(0) pos(11)) ///
 xtitle("Firm FE") xline($mean1n, lcolor(blue) lpattern(dash)) xline($mean0n, lcolor(red) lpattern(dash))
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFE_byclientnorm.pdf", replace

  twoway (hist norm if client==0 & fe<1 & fe>-2, frac color(blue%30) width(0.02)) ///
 (hist norm if client==1 & fe<1 & fe>-2, frac color(red%30) width(0.02)) ///
 , graphregion(fcolor(white) lcolor(white)) ///
 legend(label(1 "Never Using Firms") label(2 "Ever Using Firms") cols(1) ring(0) pos(11)) ///
 xtitle("Firm FE") xline($mean1n, lcolor(blue) lpattern(dash)) xline($mean0n, lcolor(red) lpattern(dash)) ///
 ttext(.045 -2 "Mean Never Using Firms= $mean0n", place(e) ) ///
ttext(.04 -2 "Mean Ever Using Firms= $mean1n", place(e)) 
 graph export  "C:\Users\Public\Documents\Data\FICUS\firmFE_byclientnorm_mean.pdf", replace

 
 *******************************
 * Wage outcome for Tab 2. 
 *******************************

 clear all 
 
 global postable EA EB EC ED EE EF EH EG 
 
 use "C:\Users\Public\Documents\Data\FICUS\sal2003_12.dta", clear
 keep dept s_brut nbheur nes16
 g year=2003
 g postable=0 
 foreach y in $postable{
 replace postable=1 if nes16=="`y'"
 }
 g w=s_brut/nbheur
 collapse (mean) w, by(postable dept)
 g year=2003
 save "C:\Users\Public\Documents\Data\FICUS\sal2003_dep.dta", replace

 global postable AZ C1 C2 C3 C4 C5 DE FZ 

 
 use "C:\Users\Public\Documents\Data\FICUS\sal2015_12.dta", clear
 keep dept s_brut nbheur a17
 g year=2003
 g postable=0 
 foreach y in $postable{
 replace postable=1 if a17=="`y'"
 }
 g w=s_brut/nbheur
 collapse (mean) w, by(postable dept)
 g year=2015
 save "C:\Users\Public\Documents\Data\FICUS\sal2015_dep.dta", replace
 
 use "C:\Users\Public\Documents\Data\FICUS\sal2003_dep.dta", clear 
 append using "C:\Users\Public\Documents\Data\FICUS\sal2015_dep.dta"
 drop if dept=="2A"
 drop if dept=="2B"
 drop if dept=="ZZ"
 destring dept, replace
 reshape wide w, i(dept postable) j(year)
 rename dept code_insee 
 merge m:1 code_insee using "C:\Users\Public\Documents\Data\FD_Figure5.dta"
 keep if _m==3
 g deltaw=(log(w2015)-log(w2003))*100
 ivreg2 deltaw (logexposure_post_pp=logexposure_pre_pp) s_ind s_ouvrier s_postable2003 if postable==1, ro
 outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\wageiv.tex", replace
 ivreg2 deltaw (logexposure_post_pp=logexposure_pre_pp) s_nms s_ind s_ouvrier s_postable2003 if postable==1, ro
 outreg2 using  "C:\Users\Public\Documents\Data\SIPSI\wageiv.tex", replace

 
 
	
	
	
	
	
	
	******
